
$(document).ready(function () {
    $.validator.setDefaults({
        errorClass: "uk-text-danger",
        errorPlacement: function (error, element) {
            error.insertAfter(element.parent("div"));
        },
        highlight: function (element, errorClass, validClass) {
            $(element).addClass("uk-form-danger");
        },
        unhighlight: function (element, errorClass, validClass) {
            $(element).removeClass("uk-form-danger");
        }
    });

    AccountSearchBox.setupFormValidation();
    $('#btn_search').click(onSearch);
    $('#btn_reset').click(AccountSearchBox.resetForm);
});

function defaultAjaxFailureHandler(jqXHR) {
    try {
        var response = JSON.parse(jqXHR.responseText);
        if (response != null && response.error != null) {
            UIkit.modal.alert("发生错误: " + response.error.message);
        } else {
            UIkit.modal.alert("发生错误。");
        }
    } catch (e) {
        console.error(e);
        UIkit.modal.alert("发生错误。")
    }
}

var blockingModal;
function onSearch() {
    AccountSearchBox.doSearch(
        function () {
            blockingModal = UIkit.modal.blockUI("正在查询...");
        },
        function (data) {
            accountList.clear();
            try {
                if (data != null && data.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        var account = data[i];
                        accountList.add(account);
                    }
                } else {
                    UIkit.notify("未找到用户");
                }
            } catch (e) {
                console.error(e);
            }
        },
        defaultAjaxFailureHandler,
        function () {
            blockingModal.hide();
        }, false);
}

function accountToTableRow(account, index) {
    var username = null;
    var userType = null;
    var name = null;
    var active = account.user.active;
    if (account.student != null) {
        username = account.student.studentNumber;
        userType = "学生";
        name = account.student.name;
    } else {
        username = account.teacher.teacherNumber;
        userType = "教师";
        name = account.teacher.name;
    }


    var activeCell;
    var button;
    var buttonCell;
    if (active) {
        activeCell = $("<td>正常</td>");
        button = $("<button class=\"uk-button uk-button-small uk-button-danger\">停用</button>");
        button.click(disableAccount);
        buttonCell = $("<td></td>").append(button);
    } else {
        activeCell = $("<td>已停用</td>");
        button = $("<button class=\"uk-button uk-button-small uk-button-danger\" disabled>停用</button>");
        buttonCell = $("<td></td>").append(button);
    }

    var tableRow = $("<tr></tr>")
        .append($("<td class=\"index-table-cell\">" + index + "</td>"))
        .append("<td>" + userType + "</td>")
        .append("<td>" + username + "</td>")
        .append("<td>" + name + "</td>")
        .append(activeCell)
        .append(buttonCell);

    return tableRow;
}

function disableAccount() {
    var index = parseInt($(this).parent().prevAll().last().text()) - 1;
    var account = accountList.accounts[index];

    if (account == null) {
        return;
    }

    UIkit.modal.confirm('确认停用该用户？', function () {
        blockingModal = UIkit.modal.blockUI('正在停用...');
        $.ajax({
            url: '/api/admin/inactivate_account',
            data: {username: account.user.username},
            type: 'POST'
        })
            .done(function () {
                UIkit.notify('已成功停用', {status: 'success'});
                account.user.active = false;
                accountList.replace(account, index);
            })
            .fail(defaultAjaxFailureHandler)
            .always(function () {
                blockingModal.hide();
            })
    });

}

